//
//  CarShipTaxCalculate.m
//  EasyToo
//
//  Created by YuanDee on 13-9-16.
//
//  车船税滞纳金=年应纳税额×(5/10000)×逾期天数，逾期应缴纳的税款金额为车船税本金加滞纳金

#import "CarShipTaxCalculate.h"

@implementation CarShipTaxCalculate

-(id)initWithLatestPaidYear:(int)year andDisplacement:(float)displacement
{
    self = [super init];
    if(self)
    {
        //获取当前时区的年份
        NSDate *date = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date];
        NSDate *now = [date  dateByAddingTimeInterval: interval];
        NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        unsigned units  = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
        NSDateComponents *comp = [myCal components:units fromDate:now];
        int currentYear = [comp year];

        self.inputYear = year;
        self.calcYear = year+1;
        self.taxByYear = [[NSMutableArray alloc] init];
        self.currentYear = currentYear;
        self.latestPaidYear = year;
        self.overdueYear = (float)(self.currentYear - self.latestPaidYear - 1);
        NSLog(@"ioverDueyear:%d\n", self.overdueYear);
        self.isOverdue = (currentYear - year)>1 ? YES : NO;
        self.displacement = displacement;
        
        //计算逾期天数，逾期年数＊365+今年已过天数
        NSString *temp = [NSString stringWithFormat:@"%d-01-01", self.currentYear];
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *firstDateOfYear=[dateFormatter dateFromString:temp];
        [dateFormatter release];
        NSTimeInterval dayInterval = [now timeIntervalSinceDate:firstDateOfYear];
        self.overdueDay = (self.overdueYear-1)*365 + dayInterval/3600/24;
        NSLog(@"self.overdueDay:%d\n", self.overdueDay);
    }
    NSLog(@"self.currentyear:%d\n", self.currentYear);
    NSLog(@"self.lastpayyear:%d\n", self.latestPaidYear);
    NSLog(@"self.oweyear:%d\n", self.overdueYear);
    NSLog(@"is owe:%c\n", self.isOverdue);
    
    


    return self;
}

-(BOOL)calculateTax
{
    
    float currentPrincipal = 0.0;
    float lateFee = 0.0;
    if(self.inputYear >= self.currentYear)
        return NO;
    
    for (; self.overdueYear>=0; self.overdueYear--)
    {
        if(self.calcYear <= 2011)
            currentPrincipal = 420.0;
        else
        {
            if(self.displacement > 4.0)
                currentPrincipal = 4500.0;
            else if(self.displacement > 3.0 && self.displacement <= 4.0)
                currentPrincipal = 3000.0;
            else if(self.displacement > 2.5 && self.displacement <= 3.0)
                currentPrincipal = 1800.0;
            else if(self.displacement > 2.0 && self.displacement <= 2.5)
                currentPrincipal = 720.0;
            else if(self.displacement > 1.6 && self.displacement <= 2.0)
                currentPrincipal = 420.0;
            else if(self.displacement > 1.0 && self.displacement <=1.6)
                currentPrincipal = 360.0;
            else if(self.displacement <= 1.0)
                currentPrincipal = 180.0;
            else
            {
                NSLog(@"Car displacement is invalid!!!");
                return NO;
            }
        }
        NSLog(@"======== overdueday:%d\n", self.overdueDay);
        if(self.overdueDay>0)
        {
            //车船税滞纳金=年应纳税额×(5/10000)×逾期天数，逾期应缴纳的税款金额为车船税本金加滞纳金
            lateFee = currentPrincipal * (5.0/10000.0) * (self.overdueDay);
        }
        else
            lateFee = 0;
        CarShipTaxInfo *info = [[[CarShipTaxInfo alloc] init]autorelease];
        info.year = [NSString stringWithFormat:@"%d", self.currentYear-self.overdueYear];
        info.lateFee = [NSString stringWithFormat:@"%f", lateFee];
        info.principal = [NSString stringWithFormat:@"%f", currentPrincipal];
        info.totalFee = [NSString stringWithFormat:@"%f", currentPrincipal+lateFee];
        [self.taxByYear addObject:info];
        NSLog(@"self.overdueDay:%d, current:%d, calcyear:%d\n", self.overdueDay, self.currentYear, self.calcYear);
        self.overdueDay = self.overdueDay - (self.currentYear - self.calcYear-self.overdueYear+1)*365;
        self.calcYear++;
        NSLog(@"info.year:%@\n", info.year);
        NSLog(@"info.latefee:%@\n", info.lateFee);
        NSLog(@"info.principal:%@\n", info.principal);
        NSLog(@"info.totalfee:%@\n", info.totalFee);

    }
//    [info release];
//    info = nil;
    return YES;
}

-(void)dealloc
{
    [super dealloc];
    [self.taxByYear release];
    self.taxByYear = nil;
}

@end
